package game.metalmax.factory
{
	import flash.geom.Point;
	
	import game.metalmax.animation.BoxSOB;
	import game.metalmax.animation.DoorSOB;
	import game.metalmax.animation.NpcSOB;
	import game.metalmax.animation.TankSOB;
	import game.metalmax.entity.character.Door;
	import game.metalmax.entity.character.Npc;
	import game.metalmax.entity.character.Tank;
	import game.metalmax.entity.world.Box;
	
	import showtime.framework.engine.map.MapObjectType;
	import showtime.framework.engine.map.rpg.ObjectHolder;
	import showtime.framework.object.Item;
	

	public class SOBFactory implements IFactory
	{
		public function SOBFactory()
		{
		}
		
		public function create(holder:ObjectHolder):Object
		{
			switch (holder.type)
			{
				case MapObjectType.NPC:
				{
					var npc:Npc = new Npc();
					wrappDynamicObjectHolder(npc, holder);			
					return new NpcSOB(npc);
				}
				case MapObjectType.DOOR:
				{
					var door:Door = new Door();					
					wrappDynamicObjectHolder(door, holder);					
					return new DoorSOB(door);
					
				}				
				case MapObjectType.VEHICLE:
				{
					var tank:Tank = new Tank();
					wrappDynamicObjectHolder(tank, holder);						
					return new TankSOB(tank);
				}
				case MapObjectType.ITEM:
				{
					switch (holder.properties["subType"])
					{
						case Item.BOX:
						{
							var object:Box = new Box();
							wrappDynamicObjectHolder(object, holder);							
							return new BoxSOB(object);
						}
					}					
				}
				default:
				{
					return null;
				}
			}
			
		}
		
		private static function wrappStaticObjectHolder(object:Object, holder:ObjectHolder):void
		{
			
		}
		
		private static function wrappDynamicObjectHolder(object:Object, holder:ObjectHolder):void
		{
			// Common properties.
			object.id = holder.name;
			object.name = holder.properties["name"];
			object.type = holder.type;
			object.subType = holder.properties["subType"]
			object.spawnPoint = new Point(holder.x, holder.y);
			object.graphicID = holder.properties["graphicID"];
			object.animationName = holder.properties["animationName"];
			object.animationConfigID = holder.properties["animationConfigID"];
			
			// Special properties.
			if (object.hasOwnProperty("dialogID"))
			{
				object["dialogID"] = holder.properties["dialogID"];
			}
			
			if (object.hasOwnProperty("aiConfigID"))
			{
				object["aiConfigID"] = holder.properties["aiConfigID"];
			}
			
			object.solid = holder.properties["solid"].toString().toLowerCase() == "true" ? true : false;		
			object.visible = holder.properties["visible"].toString().toLowerCase() == "true" ? true : false;
		}
	}
}